package org.rkuk.kasutil.test.mediaStream 
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IEventDispatcher;
	import flash.events.MouseEvent;
	import flash.events.NetStatusEvent;
	import flash.media.Video;
	import flash.net.NetConnection;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import org.rkuk.kasutil.media.MediaSourceEvent;
	import org.rkuk.kasutil.media.MediaStream;
	import org.rkuk.kasutil.test.TestBed;
	import org.rkuk.kasutil.test.TestCase;
	
	/**
	 * ...
	 * @author rkuk
	 */
	internal class MediaStreamTestCase extends TestCase 
	{
		private var streamUrl:String;
		private var connectionCmd:String;
		private var connection:NetConnection;
		private var mediaStream:MediaStream;
		
		public function MediaStreamTestCase(streamUrl:String, connectionCmd:String, testBed:TestBed, target:IEventDispatcher = null)
		{
			super(testBed, target);
			this.streamUrl = streamUrl;
			this.connectionCmd = connectionCmd;
		}
		
		override public function initialize():void 
		{
			connection = new NetConnection();
			connection.addEventListener(NetStatusEvent.NET_STATUS, onConnected);
			connection.connect(connectionCmd);
		}
		
		private function onConnected(e:NetStatusEvent):void
		{
			if (e.info.code == "NetConnection.Connect.Success")
			{
				mediaStream = new MediaStream(connection,true);
				addMediaStreamListener();
				mediaStream.streamName = streamUrl;
			}
		}
		
		private function addMediaStreamListener():void
		{
			mediaStream.addEventListener(MediaSourceEvent.READY, onMediaStreamEvent);
			mediaStream.addEventListener(MediaSourceEvent.BUFFER, onMediaStreamEvent);
			mediaStream.addEventListener(MediaSourceEvent.STOP, onMediaStreamEvent);
			mediaStream.addEventListener(MediaSourceEvent.END, onMediaStreamEvent);
			mediaStream.addEventListener(MediaSourceEvent.PAUSE, onMediaStreamEvent);
			mediaStream.addEventListener(MediaSourceEvent.PLAY, onMediaStreamEvent);
			mediaStream.addEventListener(MediaSourceEvent.RESUME, onMediaStreamEvent);
			mediaStream.addEventListener(MediaSourceEvent.SEEK, onMediaStreamEvent);
			mediaStream.addEventListener(MediaSourceEvent.RESET, onMediaStreamEvent);
		}
		
		private function onMediaStreamEvent(e:Event):void
		{
			if (e.type == MediaSourceEvent.READY)
			{
				createVideo();
				createUI();
				super.initialize();
				debugTrace(mediaStream.bytesLoaded,mediaStream.bytesTotal);
			}
			if (e.type == MediaSourceEvent.SEEK)
				debugTrace(e.type + " " + mediaStream.time);
			else
				debugTrace(e.type);
		}
		
		private function createVideo():void 
		{
			var video:Video = new Video(mediaStream.width, mediaStream.height);
			video.attachNetStream(mediaStream);
			testBed.addChild(video);
		}
		
		private function createUI():void
		{
			var bar:Sprite = new Sprite();
			bar.y = mediaStream.height;
			createTextField("close",bar);
			createTextField("play",bar);
			createTextField("pause",bar);
			createTextField("resume",bar);
			createTextField("seek", bar);
			
			testBed.addChild(bar);
		}
		
		private function createTextField(label:String,bar:Sprite):void
		{
			var txt:TextField = new TextField();
			txt.autoSize = TextFieldAutoSize.LEFT;
			txt.selectable = false;
			txt.border = true;
			txt.text = label;
			txt.x = bar.width;
			txt.addEventListener(MouseEvent.CLICK, executeCmd);
			
			bar.addChild(txt);
		}
		
		private function executeCmd(e:MouseEvent):void
		{
			var txt:TextField = e.target as TextField;
			var label:String = txt.text;
			if (label == "seek")
			{
				var offset:Number = Math.random() * mediaStream.duration;
				debugTrace("seek " + offset)
				mediaStream.seek(offset);
			}
			else
				mediaStream[label]();
		}
		
		override public function run():void 
		{
			testBed.stage.addEventListener(MouseEvent.CLICK, resetStream);
			//mediaStream.pause();
		}
		
		private function resetStream(e:MouseEvent):void
		{
			if (e.target == testBed.stage)
				mediaStream.dispose();
		}
	}

}